package cn.zxl.spring.holiday.job.web.controller;

import cn.zxl.spring.holiday.job.domain.Department;
import cn.zxl.spring.holiday.job.qo.QueryObject;
import cn.zxl.spring.holiday.job.service.DepartmentService;
import cn.zxl.spring.holiday.job.web.json.JsonResult;
import cn.zxl.spring.holiday.job.web.log.Log;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bofeifei
 * @since 2021-10-01
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequiresPermissions(value = {"部门展示","department:list"},logical = Logical.OR)
    @Log(title = "部门展示")
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model) {
        PageInfo<Department> query = departmentService.query(qo);
        model.addAttribute("pageInfo",query);
        return "department/list";
    }

    @ResponseBody
    @RequiresPermissions(value = {"部门删除","department:delete"},logical = Logical.OR)
    @Log(title = "部门删除")
    @RequestMapping("/delete")
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                departmentService.removeById(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMsg("请求失败");
        }
        return jsonResult;
    }

    @RequiresPermissions(value = {"部门编辑","department:saveOrUpdate"},logical = Logical.OR)
    @Log(title = "部门编辑")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department) {
        if (department.getId() != null) {
            departmentService.updateById(department);
            return "redirect:/department/list";
        }
        departmentService.save(department);
        return "redirect:/department/list";
    }

    @RequestMapping("/poinOut")
    public String poinOut() {
        return "";
    }

}

